//package com.superhelper.app.service;
//
//import java.util.Properties;
//
//import javax.annotation.PreDestroy;
//
//import org.apache.commons.lang3.RandomStringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//
//import com.aliyun.openservices.ons.api.Message;
//import com.aliyun.openservices.ons.api.ONSFactory;
//import com.aliyun.openservices.ons.api.Producer;
//import com.aliyun.openservices.ons.api.PropertyKeyConst;
//import com.aliyun.openservices.ons.api.SendResult;
//import com.superhelper.common.Consts;
//import com.superhelper.common.domain.baidu.BdPushEntity;
//import com.superhelper.common.domain.ele.ElePushEntity;
//import com.superhelper.common.domain.mt.MtOrderPushEntity;
//import com.superhelper.common.utils.JsonHelper;
//
//@Service
//public class MQProducerService {
//    private static final Logger log = LoggerFactory.getLogger(MQProducerService.class);
//
//    private volatile Producer eleProducer = null;
//    private volatile Producer mtProducer = null;
//    private volatile Producer bdProducer = null;
//
//    private Object eleMonitor = new Object();
//    private Object mtMonitor = new Object();
//    private Object bdMonitor = new Object();
//
//    @Value("${aliyun.access.key}")
//    private String aliyunAccessKey;
//    @Value("${aliyun.access.secret}")
//    private String aliyunSecretKey;
//
//    @Value("${mt.order.producer.id}")
//    private String mtProducerId;
//    @Value("${mt.order.topic}")
//    private String mtOrderTopic;
//
//    @Value("${ele.order.producer.id}")
//    private String eleProducerId;
//    @Value("${ele.order.topic}")
//    private String eleOrderTopic;
//
//    @Value("${bd.order.producer.id}")
//    private String bdProducerId;
//    @Value("${bd.order.topic}")
//    private String bdOrderTopic;
//    @Value("${online}")
//    private boolean online;
//
//    public Producer getEleOrderProducer() {
//        if (eleProducer == null) {
//            synchronized (eleMonitor) {
//                if (eleProducer == null) {
//                    Properties properties = new Properties();
//                    properties.put(PropertyKeyConst.AccessKey, aliyunAccessKey);
//                    properties.put(PropertyKeyConst.SecretKey, aliyunSecretKey);
//                    properties.put(PropertyKeyConst.ProducerId, eleProducerId);
//                    eleProducer = ONSFactory.createProducer(properties);
//                    eleProducer.start();
//                }
//            }
//        }
//        return eleProducer;
//    }
//
//    public Producer getMtOrderProducer() {
//        if (mtProducer == null) {
//            synchronized (mtMonitor) {
//                if (mtProducer == null) {
//                    Properties properties = new Properties();
//                    properties.put(PropertyKeyConst.AccessKey, aliyunAccessKey);
//                    properties.put(PropertyKeyConst.SecretKey, aliyunSecretKey);
//                    properties.put(PropertyKeyConst.ProducerId, mtProducerId);
//                    mtProducer = ONSFactory.createProducer(properties);
//                    mtProducer.start();
//                }
//            }
//        }
//        return mtProducer;
//    }
//
//    public Producer getBdOrderProducer() {
//        if (bdProducer == null) {
//            synchronized (bdMonitor) {
//                if (mtProducer == null) {
//                    Properties properties = new Properties();
//                    properties.put(PropertyKeyConst.AccessKey, aliyunAccessKey);
//                    properties.put(PropertyKeyConst.SecretKey, aliyunSecretKey);
//                    properties.put(PropertyKeyConst.ProducerId, bdProducerId);
//                    bdProducer = ONSFactory.createProducer(properties);
//                    bdProducer.start();
//                }
//            }
//        }
//        return bdProducer;
//    }
//
//    public boolean sendEleOrder(ElePushEntity entity) {
//        String tag = eleOrderTopic + online;
//        Message msg = new Message(eleOrderTopic, tag, JsonHelper.toJson(entity).getBytes(Consts.UTF8_CHARSET));
//        msg.setKey("ele-" + entity.getRequestId());
//        SendResult sr = getEleOrderProducer().send(msg);
//        String msgId = sr.getMessageId();
//        log.info("[sendEleOrder] success, key:{},msgId:{}", msg.getKey(), msgId);
//        return true;
//    }
//
//    public boolean sendMtOrder(Long orderId, String pushType, MtOrderPushEntity entity) {
//        String tag = mtOrderTopic + online;
//        Message msg = new Message(mtOrderTopic, tag, JsonHelper.toJson(entity).getBytes(Consts.UTF8_CHARSET));
//        msg.setKey("mt-" + orderId);
//        SendResult sr = getMtOrderProducer().send(msg);
//        String msgId = sr.getMessageId();
//        log.info("[sendMtOrder]  success, key:{},msgId:{}", msg.getKey(), msgId);
//        return true;
//    }
//
//    public boolean sendBdOrder(BdPushEntity entity) {
//        String tag = bdOrderTopic + online;
//        Message msg = new Message(bdOrderTopic, tag, JsonHelper.toJson(entity).getBytes(Consts.UTF8_CHARSET));
//        msg.setKey("bd-" + entity.getTimestamp() + "-" + RandomStringUtils.randomNumeric(5));
//        SendResult sr = getBdOrderProducer().send(msg);
//        String msgId = sr.getMessageId();
//        log.info("[sendBdOrder]  success, key:{},msgId:{}", msg.getKey(), msgId);
//        return true;
//    }
//
//    @PreDestroy
//    public void destroy() {
//        if (mtProducer != null) {
//            mtProducer.shutdown();
//        }
//        if (eleProducer != null) {
//            eleProducer.shutdown();
//        }
//    }
// }
